<?php

require_once("config.php");
  // 专门用来写封装函数的文件
header("Content-Type:text/html;charset=utf-8");
//添加
$pre_ = "pre_";
Class Person{
    private $id;
    private $nickname;
    private $mobile;
    private $email;
    private $avatar;
    private $address;
    private $createtime;
    private $depid;
    private $jobid;
    //有参构造方法
    public function __construct($id,$nickname,$mobile,$email,$avatar,$address,$createtime,$depid,$jobid)
    {
        $this->id = $id;
        $this->nickname = $nickname;
        $this->mobile = $mobile;
        $this->email = $email;
        $this->avatar = $avatar;
        $this->address = $address;
        $this->createtime = $createtime;
        $this->depid = $depid;
        $this->jobid = $jobid;
    }
    // //get set方法
 
    public function insert($tabName,$filed){
        // 引入全局变量
        global $connect;
        global $pre_;
        // 书写sql语句 
        $sql = "INSERT INTO {$pre_}{$tabName} (`{$filed['nickname']}`,`{$filed['mobile']}`,`{$filed['depid']}`,`{$filed['jobid']}`) VALUES ('{$this->nickname}','{$this->mobile}','{$this->depid}','{$this->jobid}')";
        // 执行这个语句返回一个字符串
        $res =  @mysqli_query($connect,$sql);
        // 判断查询语句是否正确
        if(!$res){
            echo "【sql执行错误】:".mysqli_error($connect);
            exit;
        }
        // 获取自增主键 返回最后一个查询中自动生成的 ID     
        return mysqli_insert_id($connect);
    }
    //修改
    public function update($tabName,$filed,$id){
        global $connect;
        global $pre_;
        $sql = "UPDATE {$pre_}{$tabName} SET `{$filed['nickname']}` = '{$this->nickname}' WHERE `id` > {$id}";
        $res = mysqli_query($connect,$sql);
        if(!$res){
            echo "【sql执行错误】:".mysqli_error($connect);
            exit;
        }
        //返回操作成功的行数
        $rows = mysqli_affected_rows($connect);
        return $rows;
    }
    //删除
    public function delete($tabName,$id){
        global $connect;
        global $pre_;
        $sql = "DELETE FROM {$pre_}{$tabName} WHERE `id` = {$id}";
        $res = mysqli_query($connect,$sql);
    
        if(!$res){
            echo "【sql执行错误】:".mysqli_error($connect);
            exit;
        }
        return mysqli_affected_rows($connect);
    }
    //单查询
    public function select($tabName=''){
        global $connect;
       
        global $pre_;
        //单查询
        $sql = "SELECT * FROM {$pre_}{$tabName}";
        $res = mysqli_query($connect,$sql);
        if(!$res){
            echo "【sql执行错误】:".mysqli_error($connect);
            exit;
        }
        //返回关联数组对象
        $data = [];
        //使用while循环来拿数据  $rows = mysqli_fetch_assoc( $res) 每次拿一挑数据 直到拿到最后一条数据停
        while($rows = mysqli_fetch_assoc( $res))
        {
            $data[] = $rows;
        }
        return $data;
    }   
    //多表查询
    public function selectAll(){
        global $connect;
        global $pre_;
        $sql = "SELECT person.*,dept.name AS deptname,job.name AS jobname FROM {$pre_}{person} AS person LEFT JOIN {$pre_}department AS dept ON dept.id = person.depid LEFT JOIN {$pre_}job AS job ON job.id = person.jobid";
        $res = mysqli_query($connect,$sql);
        if(!$res){
            echo "【sql执行错误】:".mysqli_error($connect);
            exit;
        }
        //返回关联数组对象
        $data = [];
        //使用while循环来拿数据  $rows = mysqli_fetch_assoc( $res) 每次拿一挑数据 直到拿到最后一条数据停
        while($rows = mysqli_fetch_assoc( $res))
        {
            $data[] = $rows;
        }
        return $data;
    }


}